# -- coding:utf-8 --

# 正则表达式的使用

# 1

title = "\n 1 正则表达式的使用"
print(title)
import re
str = "www.runoob.com"
print(re.match('www',str).span())
# match 从开头匹配
print(re.match('com',str))


# 2 match方法
title = "\n 2 match方法"
print(title)
import re
line = "Cats are smarter than dogs"
matchObj = re.match(r'(.*) are (.*?) .*', line,re.M|re.I)

if matchObj :
	print("matchObj.group=" + matchObj.group())
	print("matchObj.group(1)=" + matchObj.group(1))
	print("matchObj.group(2)=" + matchObj.group(2))
	
else :
	print("No matchs")


#  3search方法
title = "\n 3 search方法"
print(title)
import re
str = "www.runoob.com"
print(re.search('www',str).span())
# match 从开头匹配
print(re.search('com',str).span())

# 4 match方法
title = "\n 4 match 与search的区别"
print(title)
import re
line = "Cats are smarter than dogs"
matchObj = re.match(r'dogs', line,re.M|re.I)

if matchObj :
	print("matchObj.group=" + matchObj.group())

else :
	print("No matchs")
	
matchObj = re.search(r'dogs', line,re.M|re.I)

if matchObj :
	print("matchObj.group=" + matchObj.group())

else :
	print("No matchs")
	
	
	
# 5 检索和替换
title = "\n 5 检索和替换"
print(title)
import re

phone = "2004-959-559 # 这是一个电话号码"
print(phone)
# 删除数字
num = re.sub(r'#.*$', "", phone)
print(num)

# 删除非数字内容
num = re.sub(r'\D', "", phone)
print(num)

# 6 complie
title = "\n 6 compile函数"
print(title)

import re
p = re.compile(r'\d+')
str = "one12two3three"
m = p.match(str)
print(m)

m = p.match(str,3)
print(m)
print(m.group(0))


